package com.evernote.controller;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.evernote.entity.Content;
import com.evernote.entity.NoteBook;
import com.evernote.entity.User;
import com.evernote.service.ContentService;
import com.evernote.service.NoteBookService;
import com.evernote.service.UserService;

/**
 * Servlet implementation class DoLogin
 */
@WebServlet("/DoLogin")
public class DoLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		String msg = "";
		boolean flag = false;
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		User u = new User();
		UserService us = new UserService();
		
		u.setUsername(username);
		u.setPassword(password);
		int Uid = 0;
		if(us.isExist(u)) {//判断用户名是否存在
			Uid = us.login(u);
			if(Uid != 0) {
				msg = "登录成功";
				flag = true;
			} else {
				msg = "密码错误";
			}
		} else {
			msg = "用户名不存在";
		}
		request.setAttribute("msg", msg);
		if(flag) {
			HttpSession session = request.getSession();
			session.setAttribute("username", username);
			
			request.setAttribute("Uid", Uid);
			request.getRequestDispatcher("ToIndex").forward(request, response);
		}else {
			request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
